<template>
  <div class="app-container home">
    <div class="search">
      <el-form :model="queryParams"
               ref="queryForm"
               size="small"
               :inline="true"
               v-show="showSearch"
               label-width="68px">

        <el-form-item style="width:300px; padding-left: 30px">
          <el-input style="width: 250px"
                    v-model="queryParams.searchValue"
                    placeholder="送检单位名称/受检人姓名"
                    clearable />
        </el-form-item>


        <el-form-item>
          <el-button type="primary"
                     icon="el-icon-search"
                     size="mini"
                     @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"
                     size="mini"
                     @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-col :span="24"
            :xs="24">
      <el-table v-loading="loading"
                :data="dataList"
                width="150px"
                style="height:calc(100vh - 280px)"
                @selection-change="handleSelectionChange"
                border>
        <el-table-column type="selection"
                         sortable
                         :selectable="handleSelectable"
                         width="55"
                         align="center" />
        <el-table-column label="送检单位名称"
                         align="center"
                         show-overflow-tooltip
                         prop="sjdwName" />

        <el-table-column label="受检人姓名"
                         align="center"
                         prop="patientName" />
        <el-table-column label="受检人性别"
                         align="center"
                         prop="patientSex" />
        <el-table-column label="受检人年龄"
                         align="center"
                         prop="patientAge" />
        <el-table-column label="受检人手机号"
                         align="center"
                         prop="patientPhone" >
          <template slot-scope="scope">
            <!-- 使用计算属性将中间4位加密为*号 -->
            <span>{{ hideMiddleFour(scope.row.patientPhone) }}</span>
          </template>

        </el-table-column>

        <el-table-column label="配偶姓名"
                         align="center"
                         prop="mateName" />
        <el-table-column label="配偶性别"
                         align="center"
                         prop="mateSex" />
        <el-table-column label="配偶年龄"
                         align="center"
                         prop="mateAge" />

        <el-table-column label="操作"
                         align="center"
                         class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini"
                       icon="el-icon-warning"
                       style="background-color: #fff; border-color: #fff; color: #aa2290;"
                       v-hasPermi="['check:patient:query']"
                       @click="details(scope.row)">详情</el-button>

            <el-button size="mini"
                       type="text"
                       icon="el-icon-edit"
                       @click="handleUpdate(scope.row)"
                       v-hasPermi="['check:patient:edit']">修改</el-button>
          </template>

        </el-table-column>
      </el-table>
      <pagination v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList" />
    </el-col>
    <!-- 修改   -->
    <el-dialog title="修改信息"
               :visible.sync="isPatientOpen">
      <el-form :model="patientForm"
               ref="patientForm"
               :rules="rules">

        <el-form-item label="所属医院"
                      :label-width="formLabelWidth">
          <el-input style="width: 220px"
                    :value="patientForm.sjdwName"
                    readonly></el-input>

        </el-form-item>
<!--        <el-form-item label="选择医生"-->
<!--                      :label-width="formLabelWidth"-->
<!--                      prop="sharedIds">-->
<!--          <el-select v-model="patientForm.nickName"-->
<!--                     placeholder="请选择医生">-->
<!--            <el-option v-for="user in userOptions"-->
<!--                       :key="user.nickName"-->
<!--                       :label="user.nickName"-->
<!--                       :value="user.nickName"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="受检人手机号" prop="patientPhone" :label-width="formLabelWidth">
          <el-input style="width: 220px"
                    v-model="patientForm.patientPhone"
                    placeholder="请输入手机号" maxlength="20"/>
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   class="button"
                   @click="patientConfirm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/check/patient";
import { getPost } from '@/api/system/post'
import { getUserListByDeptName } from '@/api/system/custom'
import { shareSamples } from '@/api/check/sample'
import { updateItem } from '@/api/check/item'

export default {
  name: "Patient",
  data () {
    return {
      isPatientOpen:false,
      formLabelWidth: "100px",
      userOptions: [],

      isShow:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue:""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientPhone: [
          {
            required:false,
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号格式错误",
            trigger: "blur",
          },
        ],
      },

      patientForm:{
        sjdwName:undefined,
        patientPhone:undefined,
        nickName:undefined,
        id:undefined
      }

    };

  },

  created () {
    var isRegister = this.$store.state.user.isRegister;
    if (isRegister == null || isRegister == 0) {//代表后台
      this.isShow = true;
    } else {
      this.isShow = false;
    }
    this.getList();
  },
  methods: {

    patientConfirm: function () {
      var that = this;
      that.$refs["patientForm"].validate(valid => {
        if (valid) {
          var form = that.patientForm;
          updatePatient(form).then(response => {
            //需要判断当前传入的nickName是否有deptId，没有的话提示
            this.$modal.msgSuccess("修改成功");
            this.isPatientOpen = false;
            this.getList();
          });
        }
      });
    },

    getUser () {
      var that = this
      var form = {};
      form.deptName = that.patientForm.sjdwName ;
      getUserListByDeptName(form).then(response => {
        if (response.code == '200' && response.data) {
          that.userOptions = response.data
        }
      });
    },

    share: function () {
      var form = {};
      var that = this;
      that.form = form;
      var ids = this.ids
      if (ids == null || ids.length == 0) {
        this.$modal.msgError("请先选择要分享的数据");
        return false;
      } else {
        that.isShareOpen = true
      }

    },

    hideMiddleFour(phone) {
      if(phone){
        return phone.replace(/(\d{3})\d*(\d{4})/, '$1****$2');
      }

    },
    handleSelectable (row) {
      if (row.sjys == this.$store.state.user.nickName) {
        return true;
      } else {
        return false;
      }
    },
    details (row) {
      this.$router.push({
        path: '/check/patient/detail',
        query: { id: row.id }
      })
    },

    /** 查询患者列表 */
    getList () {
      this.loading = true;
      listPatient(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
      };
      this.resetForm("form");
    },

    patientReset(){
      this.patientForm = {
        sjdwName:undefined,
        patientPhone:undefined,
        nickName:undefined,
        id:undefined
      }
      this.resetForm("patientForm");
    },

    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams.searchValue = undefined
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加患者";
    },

    /** 修改按钮操作 */
    handleUpdate (row) {
      this.patientReset();
      const id = row.id || this.ids
      this.isPatientOpen = true;
      this.patientForm.sjdwName = row.sjdwName;
      this.patientForm.id = id
      this.title = "修改患者";
      this.getUser();
    },


    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePatient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPatient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除患者编号为"' + ids + '"的数据项？').then(function () {
        return delPatient(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
  },

};
</script>
<style lang="scss" scoped>
.app-container {
  padding: 18px 21px 0 21px;
}
.home {
  width: 100%;
  height: calc(100% - 50px);

  .search {
    float: right;
  }
}
</style>
